package com.example.dollycontrol;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

@SuppressLint("DrawAllocation")
public class EasingFunction extends View {

	private Paint paint = new Paint();
	private Path path;
	private DollySlider slider;
	
	/*
	 * constructors
	 */
	
	public EasingFunction(Context context) {
		super(context);
	}
	
	public EasingFunction(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	
	public EasingFunction(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}
	
	@Override
	protected void onDraw(Canvas canvas){
		
		paint.setColor(Color.rgb(51, 181, 229));
		paint.setStyle(Style.STROKE);
		paint.setAntiAlias(true);
		paint.setStrokeCap(Paint.Cap.ROUND);
		paint.setStrokeWidth(2.0f);
						
		path = new Path();
		
		float handleMin = slider.getAbsoluteFromPercentage(slider.getMinHandle());
		float handleMax = slider.getAbsoluteFromPercentage(slider.getMaxHandle());
		//float center = (handleMax - handleMin)/2;
		float easeInMarker = 0;
		float easeOutMarker = 0;
		
		for(SlideMark sM : slider.getSlideMark()){
			if(sM.getHandler().equals(Handler.EASE_IN)){
				easeInMarker = slider.getAbsoluteFromPercentage(sM.getPosition());
			}
			else if(sM.getHandler().equals(Handler.EASE_OUT)){
				easeOutMarker = slider.getAbsoluteFromPercentage(sM.getPosition());
			}
		}
		
		//constructing from top right
		path.moveTo(handleMax,0);
		path.cubicTo(handleMin + easeInMarker, 0, handleMin + easeOutMarker, 200, handleMin, 200);
		canvas.drawPath(path, paint);
	}
	
	public void setSliderReference(DollySlider slider){
		this.slider = slider;
	}
	
	public void sliderHasChangedNotification(Notification eventType){
		if(eventType.equals(Notification.DRAG_EVENT)){
			invalidate();
		}
	}
}
